package com.itheima.bos.web.action.base;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;
import org.springframework.data.domain.Page;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.util.CompoundRoot;
import com.opensymphony.xwork2.util.ValueStack;

public abstract class BaseAction<T> extends ActionSupport implements ModelDriven<T> {

	protected T model;
	@Override
	public T getModel() {
		return model;
	}

	public BaseAction() {
		Type genericSuperclass = this.getClass().getGenericSuperclass();
		ParameterizedType parameterizedType = (ParameterizedType) genericSuperclass;
		Class<T> modelClass = (Class<T>) parameterizedType.getActualTypeArguments()[0];
		try {
			model = modelClass.newInstance();
		} catch (InstantiationException | IllegalAccessException e) {
			e.printStackTrace();
			System.out.println("构造模型失败");
		}
	}

	protected int page;
	protected int rows;

	public void setPage(int page) {
		this.page = page;
	}

	public void setRows(int rows) {
		this.rows = rows;
	}

	public void pushPageDataToValueStack(Page<T> pageData) {
		Map<String, Object> result = new HashMap<String, Object>(2);
		result.put("total", pageData.getTotalElements());
		result.put("rows", pageData.getContent());
		ActionContext.getContext().getValueStack().push(result);
	}

	/**
	 * 向值栈栈顶set Map数据
	 * @param key --key值
	 * @param obj --值
	 */
	public void set2ValueStack(String key, Object obj) {
		ActionContext.getContext().getValueStack().set(key, obj);
	}

	/**
	 * 向值栈栈顶set obj数据
	 * @param obj --值
	 */
	public void push2ValueStack(Object obj) {
		ActionContext.getContext().getValueStack().push(obj);
	}

	/**
	 * 向值contextMap中存入数据
	 * @param key
	 * @param value
	 */
	public void put2ContextMap(String key, Object value) {
		ActionContext.getContext().put(key, value);
	}

	/**
	 * 向值request域中存入数据
	 * @param key
	 * @param value
	 */
	public void put2Session(String key, Object value) {
		ActionContext.getContext().getSession().put(key, value);
	}

	/**
	 * 向值session域中存入数据
	 * @param key
	 * @param value
	 */
	public void put2Request(String key, Object value) {
		ServletActionContext.getRequest().setAttribute(key, value);
	}

	/**
	 * 向值application域中存入数据
	 * @param key
	 * @param value
	 */
	public void put2Application(String key, Object value) {
		ServletActionContext.getServletContext().setAttribute(key, value);
	}

	/**
	 * 向值valueStack顶存入数据
	 * @param key
	 * @param value
	 */
	public void add2ValueStack(Object value) {
		add2ValueStack(value, 0);
	}

	/**
	 * 向值valueStack的指定位置存入数据
	 * @param key
	 * @param value
	 */
	@SuppressWarnings("unchecked")
	public void add2ValueStack(Object value, int index) {
		ActionContext.getContext().getValueStack().getRoot().add(index, value);
	}

	public HttpServletRequest getRequest() {
		return ServletActionContext.getRequest();
	}

	public HttpSession getSession() {
		return ServletActionContext.getRequest().getSession();
	}

	public ServletContext getServletContext() {
		return ServletActionContext.getServletContext();
	}

	public HttpServletResponse getRespose() {
		return ServletActionContext.getResponse();
	}

	public CompoundRoot getRoot() {
		return ActionContext.getContext().getValueStack().getRoot();
	}

	public ValueStack getValueStack() {
		return ActionContext.getContext().getValueStack();
	}

	public Map<String, Object> getContextMap() {
		return ActionContext.getContext().getValueStack().getContext();
	}

}
